<?php
/**
 * Created by PhpStorm.
 * User: Edward
 * Date: 2019/6/3
 * Time: 9:35
 */

namespace app\admin\controller\shop;

use app\admin\BaseController;
use app\admin\model\ShopCategory as Category;
use app\admin\model\ShopRelationCategory;
use app\lib\exception\ExistException;
use app\lib\exception\FailedException;
use app\lib\exception\NoJurisdiction;
use app\lib\exception\NotFoundException;
use app\Validate\CategoryValidate;
use app\Validate\IdMustPositive;
use think\Request;

class ShopCategory extends BaseController {
    /**
     * 获取商家所有的主营类目
     * @param Request $request
     * @return \think\response\Json
     * @throws NotFoundException
     */
    public function categoryAll(Request $request){
        $post = $request->get();
        $where = [];
        $count = (new \app\admin\model\ShopCategory())->getCount($where);
        $where['page'] = $post['page'];
        $category = (new \app\admin\model\ShopCategory())->wechatList($where,"id ,category, create_time","id desc",$post['page_number']);
        foreach ($category as $key=>$value){
            $category[$key]['shop_number'] =(new ShopRelationCategory())->where("category_id",'eq',$value['id'])->count();
        }
        if(empty($category)){ throw new NotFoundException(['msg' => '暂无商家分类，请添加分类']); }
        return writeJson(200,[
            'category' => $category,
            'count'    =>$count
        ]);
    }

    /**
     * 新建商品分类
     * @param Request $request
     * @return \think\response\Json
     * @throws ExistException
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function createCategory(Request $request){
        (new CategoryValidate())->goCheck();
        $category = $request->category;
        $existCategory = Category::findCategoryByName($category);
        if($existCategory){
            throw new ExistException(['msg' => $category .'分类已经存在!']);
        }
        $result = Category::createCategory($category);
        if(empty($result)){
            throw new FailedException(['msg' => '新增失败，请重试']);
        }
        return writeJson(201,[]);
     }

    /**
     * 修改商品分类
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function editCategory(Request $request){
        (new IdMustPositive())->goCheck();
        (new CategoryValidate())->goCheck();
        $category = $request->category;
        $id = $request->id;
        $new = ['id'=>$id,'category'=>$category];
        $result = (new \app\admin\model\ShopCategory())->dataUpdate($new);
        if(!$result){
            throw new FailedException(['msg' => '修改失败，请重试']);
        }
        return writeJson(200,[]);
    }

    /**
     * 删除商品分类
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws NotFoundException
     * @throws \app\lib\exception\ParameterException
     * @throws \Exception
     */
    public function deleteCategory(Request $request){
        (new IdMustPositive())->goCheck();
        $id = $request->id;

        //有商家不可以删除
        $shop_number =(new ShopRelationCategory())->where("category_id",'eq',$id)->count();
        if($shop_number > 0){ throw new NotFoundException(['msg' => '当前分类有商家,不可删除']); }

        $where["id"] = $id;
        $result = (new \app\admin\model\ShopCategory())->where($where)->delete();
        if(!$result){ throw new FailedException([ 'msg' => '删除失败，请重试']); }
        return writeJson(200,['msg' => '删除成功']);
    }
}